package com.xbh.wificonfiglib.protocol;

import android.util.Log;

import com.xbh.wificonfiglib.common.Constant;
import com.xbh.wificonfiglib.security.ITouchEncryptor;
import com.xbh.wificonfiglib.task.IWifiConfigLibGenerator;
import com.xbh.wificonfiglib.util.ByteUtil;
import com.xbh.wificonfiglib.util.SimpleCipherUtil;

import java.net.InetAddress;
import java.util.Arrays;

public class WifiConfigLibGenerator implements IWifiConfigLibGenerator {

    private static final String TAG = "WifiConfigLibGenerator";
    private final byte[][] mGcBytes2;
    private final byte[][] mDcBytes2;
    private byte[] mSsid;
    private byte[] mPassword;
    private byte[] mEncryptedData;

    /**
     * Constructor of EsptouchGenerator, it will cost some time(maybe a bit
     * much)
     *
     * @param apSsid      the Ap's ssid
     * @param apBssid     the Ap's bssid
     * @param apPassword  the Ap's password
     * @param inetAddress the phone's or pad's local ip address allocated by Ap
     */
    public WifiConfigLibGenerator(byte[] apSsid, byte[] apBssid, byte[] apPassword, InetAddress inetAddress,
                                  ITouchEncryptor encryptor) {
        this(apSsid, apBssid, apPassword, false,inetAddress, encryptor);
    }
    public WifiConfigLibGenerator(byte[] apSsid, byte[] apBssid, byte[] apPassword,boolean ssidWithZh, InetAddress inetAddress,
                                  ITouchEncryptor encryptor) {
        mSsid = apSsid;
        mPassword = apPassword;
        // generate guide code
        GuideCode gc = new GuideCode();
        char[] gcU81 = gc.getU8s();
        mGcBytes2 = new byte[gcU81.length][];

        for (int i = 0; i < mGcBytes2.length; i++) {
            mGcBytes2[i] = ByteUtil.genSpecBytes(gcU81[i]);
        }

        // generate data code
        DatumCode dc = new DatumCode(apSsid, apBssid, apPassword, inetAddress, encryptor);
        char[] dcU81 = dc.getU8s();
        mDcBytes2 = new byte[dcU81.length][];

        for (int i = 0; i < mDcBytes2.length; i++) {
            mDcBytes2[i] = ByteUtil.genSpecBytes(dcU81[i]);
        }

        byte[] encryptSsid = SimpleCipherUtil.encrypt(apSsid);
        byte[] encryptBssid = SimpleCipherUtil.encrypt(apBssid);
        byte[] encryptPsw = SimpleCipherUtil.encrypt(apPassword);

        mEncryptedData = new byte[Constant.HEADER_LEN + encryptSsid.length + encryptBssid.length + encryptPsw.length];

        mEncryptedData[0] = Constant.SEND_HEARDER;
        mEncryptedData[1] = (byte) (SimpleCipherUtil.getPolyNum() & 0xff);
        mEncryptedData[2] = (byte) (encryptSsid.length & 0xff);
        mEncryptedData[3] = ssidWithZh ? (byte) (encryptBssid.length & 0xff) : 0x00;
        mEncryptedData[4] = ssidWithZh ? 0x00 :(byte) (encryptBssid.length & 0xff)  ;
        mEncryptedData[5] = (byte) (encryptPsw.length & 0xff);

        /*System.arraycopy(apSsid, 0, mEncryptedData, Constant.HEADER_LEN, apSsid.length);
        System.arraycopy(apPassword, 0, mEncryptedData, Constant.HEADER_LEN + apSsid.length, apPassword.length);

        Log.d(TAG, "WifiConfigLibGenerator: 加密前：" + ByteUtil.bytes2HexStr(mEncryptedData));*/

        System.arraycopy(encryptSsid, 0, mEncryptedData, Constant.HEADER_LEN, encryptSsid.length);
        System.arraycopy(encryptBssid, 0, mEncryptedData, Constant.HEADER_LEN + encryptSsid.length, encryptBssid.length);
        System.arraycopy(encryptPsw, 0, mEncryptedData, Constant.HEADER_LEN + encryptSsid.length + encryptBssid.length, encryptPsw.length);

        Log.d(TAG, "WifiConfigLibGenerator encrypt：" + ByteUtil.bytes2HexStr(mEncryptedData));

        /*byte[] bytes1 = Arrays.copyOf(mEncryptedData, mEncryptedData.length);

        byte[] ssid = SimpleCipherUtil.encrypt(Arrays.copyOfRange(mEncryptedData, Constant.HEADER_LEN, 4 + mSsid.length));
        byte[] psw = SimpleCipherUtil.encrypt(Arrays.copyOfRange(mEncryptedData, Constant.HEADER_LEN + mSsid.length, Constant.HEADER_LEN + mSsid.length + mPassword.length));

        System.arraycopy(ssid, 0, bytes1, Constant.HEADER_LEN, ssid.length);
        System.arraycopy(psw, 0, bytes1, Constant.HEADER_LEN + ssid.length, psw.length);
        Log.d(TAG, "WifiConfigLibGenerator: 解密后：" + ByteUtil.bytes2HexStr(bytes1));*/
    }

    @Override
    public byte[][] getGCBytes2() {
        return mGcBytes2;
    }

    @Override
    public byte[][] getDCBytes2() {
        return mDcBytes2;
    }

    @Override
    public byte[] getEncryptedData() {
        return mEncryptedData;
    }
    @Override
    public byte[] getSsid() {
        return mSsid;
    }
    @Override
    public byte[] getPassword() {
        return mPassword;
    }
}
